<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>

<body>
  <div>
    <button id="btn1">请求</button>
    <button id="btn2">取消</button>
  </div>

  <script src="https://cdn.bootcdn.net/ajax/libs/axios/0.21.1/axios.js"></script>

  <script>
    const oBtn1 = document.getElementById('btn1')
    const oBtn2 = document.getElementById('btn2')

    // 获取请求对象
    const CancelToken = axios.CancelToken

    let xhr = null;
    oBtn1.onclick = function () {
      xhr = new XMLHttpRequest();
      xhr.open("get", "http://127.0.0.1:3000/login?name=aa&pass=123&t=" + Date.now(), true)
      xhr.send();
      xhr.onreadystatechange = function () {
        if (xhr.readyState !== 4) return;
        if (xhr.status === 200) {
          console.log('success', xhr.responseText);
          return;
        }
        console.log('fail');
      }
    }
    oBtn2.onclick = function () {
      xhr.abort();
      console.log("quxiao");
    }
  </script>
</body>

</html>